/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DlgAddCategorie.java
 *
 * Created on 2011-04-09, 13:42:23
 */
package dialogs;

import entities.Categorie;
import java.awt.Color;
import java.util.ArrayList;

/**
 *
 * @author chocoseb
 */
public class DlgAddCategorie extends javax.swing.JDialog {

    private boolean _confirmed = false;

    /** Creates new form DlgAddCategorie */
    public DlgAddCategorie(java.awt.Frame parent, boolean modal, ArrayList<Categorie> p_list) {
        super(parent, modal);
        initComponents();
        cmbParent.removeAllItems();
        cmbParent.addItem("Aucun");
        for (Categorie c : p_list) {
            cmbParent.addItem(c);
        }
        this.setLocationRelativeTo(parent);
    }

    public DlgAddCategorie(java.awt.Frame parent, boolean modal, ArrayList<Categorie> p_list, Categorie courant) {
        super(parent, modal);
        initComponents();
        cmbParent.removeAllItems();
        cmbParent.addItem("Aucun");
        for (Categorie c : p_list) {
            if (!c.getName().equals(courant.getName())) {
                cmbParent.addItem(c);
                if (courant.getParent() != null) {
                    if (c.getName().equals(courant.getParent().getName())) {
                        cmbParent.setSelectedIndex(cmbParent.getItemCount() - 1);
                    }
                }
            }
        }
        txfName.setText(courant.getName());
        spnRed.setValue(courant.getColor().getRed());
        spnGreen.setValue(courant.getColor().getGreen());
        spnBlue.setValue(courant.getColor().getBlue());
        changeColor();
        this.setLocationRelativeTo(parent);
    }

    public Categorie getNewCategorie() {
        Categorie c = new Categorie();
        return getUpdatedCategorie(c);
    }

    public Categorie getUpdatedCategorie(Categorie c) {
        c.setName(txfName.getText());
        c.setColor(pnlColor.getBackground());
        Object parent = cmbParent.getSelectedItem();
        if (parent instanceof Categorie) {
            c.setParent((Categorie) parent);
        }
        return c;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txfName = new javax.swing.JTextField();
        btnClose = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        pnlColor = new javax.swing.JPanel();
        lblNameColor = new javax.swing.JLabel();
        spnRed = new javax.swing.JSpinner();
        spnBlue = new javax.swing.JSpinner();
        spnGreen = new javax.swing.JSpinner();
        cmbParent = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Nom :");

        txfName.setText("Nouveau");

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        pnlColor.setBackground(new java.awt.Color(255, 0, 0));
        pnlColor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblNameColor.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        lblNameColor.setForeground(new java.awt.Color(255, 255, 255));
        lblNameColor.setText("New Categorie");
        pnlColor.add(lblNameColor);

        spnRed.setModel(new javax.swing.SpinnerNumberModel(255, 0, 255, 5));
        spnRed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnRedStateChanged(evt);
            }
        });

        spnBlue.setModel(new javax.swing.SpinnerNumberModel(0, 0, 255, 5));
        spnBlue.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnBlueStateChanged(evt);
            }
        });

        spnGreen.setModel(new javax.swing.SpinnerNumberModel(0, 0, 255, 5));
        spnGreen.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnGreenStateChanged(evt);
            }
        });

        cmbParent.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmbParent.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbParent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbParentActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addGap(18, 18, 18)
                        .addComponent(btnClose))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmbParent, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(pnlColor, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(spnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(spnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(spnRed, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(txfName))))
                .addGap(71, 71, 71))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(spnRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(cmbParent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnAdd))
                .addGap(53, 53, 53))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void spnRedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnRedStateChanged
        changeColor();
    }//GEN-LAST:event_spnRedStateChanged

    private void spnGreenStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnGreenStateChanged
        changeColor();
    }//GEN-LAST:event_spnGreenStateChanged

    private void spnBlueStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnBlueStateChanged
        changeColor();
    }//GEN-LAST:event_spnBlueStateChanged

    private void cmbParentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbParentActionPerformed
        Object selected = cmbParent.getSelectedItem();
        if (selected instanceof Categorie) {
            cmbParent.setBackground(((Categorie) selected).getColor());
        } else {
            cmbParent.setBackground(Color.WHITE);
        }
    }//GEN-LAST:event_cmbParentActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        _confirmed = false;
        this.setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        _confirmed = true;
        this.setVisible(false);
    }//GEN-LAST:event_btnAddActionPerformed

    private void changeColor() {
        Color newColor = new Color((Integer) spnRed.getValue(), (Integer) spnGreen.getValue(), (Integer) spnBlue.getValue());
        pnlColor.setBackground(newColor);
        lblNameColor.setText(txfName.getText());
    }

    public boolean isConfirmed() {
        return _confirmed;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClose;
    private javax.swing.JComboBox cmbParent;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblNameColor;
    private javax.swing.JPanel pnlColor;
    private javax.swing.JSpinner spnBlue;
    private javax.swing.JSpinner spnGreen;
    private javax.swing.JSpinner spnRed;
    private javax.swing.JTextField txfName;
    // End of variables declaration//GEN-END:variables
}
